<template>
    <div>
        <div>
            <Form :label-width="100" inline>
                <FormItem label="行政区划" prop="areacode">
                    <area-linkage :areaCode.sync="queryParam.ssca0043" :countyDisable=false :streetDisable=false
                                  :communityDisable=false></area-linkage>
                </FormItem>
                <Form-item label="户主姓名">
                    <Input v-model="queryParam.azcp0003" placeholder="请输入户主姓名"
                           style="width: 200px"></Input>
                </Form-item>
                <Form-item label="证件号码">
                    <Input v-model="queryParam.azcp0001" placeholder="请输入证件号码"
                           style="width: 200px"></Input>
                </Form-item>
                <!--                <FormItem label="是否代领">-->
                <!--                    <dictSelect :sel_val='queryParam.ssca0082' dictCode="yesorno" :bindData.sync="queryParam.ssca0082"-->
                <!--                                style="width: 150px" clearable/>-->
                <!--                </FormItem>-->
                <Row class="button_row">
                    <span style="float: right;">
                     <Button type="primary" @click="searchQuery">查询</Button>
                     <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                     </span>
                </Row>
            </Form>
        </div>
        <Modal v-model="visible" :title="title" width="800" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
                <table style="width:100%;">
                    <tr>
                        <td>
                            <FormItem label="户主身份证号码" prop="azcp0001">
                                <Input v-model="formData.azcp0001" style="width: 200px" readonly></Input>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="户主姓名" prop="azcp0003">
                                <Input v-model="formData.azcp0003" style="width: 200px" readonly></Input>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="开户银行名称" prop="ssca0009">
                                <dictSelect :sel_val='formData.ssca0009' dictCode="bankcategory"
                                            :bindData.sync="formData.ssca0009" style="width: 200px" clearable/>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="是否代领" prop="ssca0082">
                                <dictSelect :sel_val='formData.ssca0082' dictCode="yesorno" @changeState="changeMember"
                                            :bindData.sync="formData.ssca0082" style="width: 200px" clearable/>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="开户人姓名" prop="ssca0010">
                                <Input v-model="formData.ssca0010" style="width: 200px" v-if="inputSsca0010"></Input>
                                <Select style="width:200px" placeholder="开户人姓名" v-model="formData.ssca0010" clearable
                                        v-if="selectSsca0010" @on-change="changeSsca0010">
                                    <Option v-for="item in familyMember" :value="item.azcp0003" :key="item.azcp0003">
                                        {{item.azcp0003 }}
                                    </Option>
                                </Select>

                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="开户人证件号码" prop="ssca0083">
                                <Input v-model="formData.ssca0083" style="width: 200px"
                                       @on-blur="loadExistData()"></Input>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="银行卡帐号" prop="ssca0011">
                                <Input v-model="formData.ssca0011" style="width: 200px" type="number"></Input>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="银行卡帐号确认" prop="ssca0011_check" v-if="ssca0011Check">
                                <Input v-model="formData.ssca0011_check" style="width: 200px" type="number"></Input>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <template>
                                <Upload name="ssca0095"
                                        action="/api/ac/hdplugin/ossService/uploadBase64"
                                        :max-size=300
                                        :on-exceeded-size="handleMaxSize"
                                        :before-upload="handleUpload">
                                    <Button icon="ios-cloud-upload-outline">上传代领要件</Button>
                                </Upload>
                                <img :src="temp_photo" style="width: 100%"/>
                            </template>
                        </td>
                    </tr>
                </table>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <template v-if="!disableSubmit">
                    <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
                </template>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, record, index  }" slot="action">
                    <!--                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>-->
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleDetail(index)">查看</Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
        <!--        <rule-action-infor-modal ref="modalForm" @ok="modalFormOk"/>-->
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import {IdentityCodeValid} from '@/plugins/idcardValid.js'
    import DateFormat from '@/plugins/format.js'
    import {getSign, createUpLoadFiles, compressImg} from '@/api/common'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect,
            //Ss01001Tag
        },
        data() {
            return {
                rules: {
                    ssca0009: [
                        {required: true, message: '请选择"开户银行名称"', trigger: 'blur'}
                    ],
                    ssca0082: [
                        {required: true, message: '请选择"是否代领"', trigger: 'blur'}
                    ],
                    ssca0010: [
                        {required: true, message: '请选择"开户人姓名"', trigger: 'blur'}
                    ],
                    ssca0083: [
                        {required: true, message: '请选择"开户人证件号码"', trigger: 'blur'}
                    ],
                    ssca0011: [
                        {required: true, message: '请选择"银行卡帐号"', trigger: 'blur'}
                    ],
                    ssca0011_check: [
                        {required: true, message: '请选择"银行卡帐号确认"', trigger: 'blur'}
                    ],
                },
                fieldsToValidate: ["ssca0009", "ssca0082", "ssca0010", "ssca0083", "ssca0011", "ssca0011_check"],
                disableSubmit: false,
                columns: [
                    {
                        title: '救助类型',
                        align: 'center',
                        key: 'ssca0002_name',
                        width: '150px',
                        fixed: 'left',
                    },
                    {
                        title: '属地区划',
                        align: 'center',
                        key: 'AZAA0002',
                        width: 250,
                        fixed: 'left',
                    },
                    {
                        title: '户主姓名',
                        align: 'center',
                        key: 'azcp0003',
                        width: '100px',
                        fixed: 'left',
                    },
                    {
                        title: '户主身份证号',
                        align: 'center',
                        key: 'azcp0001',
                        width: 200
                    },
                    {
                        title: '开户银行名称',
                        align: 'center',
                        key: 'ssca0009_name',
                        width: 150
                    },
                    {
                        title: '开户银行账户',
                        align: 'center',
                        key: 'ssca0010',
                        width: '120px'
                    },
                    {
                        title: '银行卡帐号',
                        align: 'center',
                        key: 'ssca0011',
                        width: 220
                    },
                    {
                        title: '代领人/开户人证件号码',
                        align: 'center',
                        key: 'ssca0083',
                        width: 200
                    },
                    {
                        title: '数据状态',
                        align: 'center',
                        key: 'bpm_status_name',
                        width: 120
                    },
                    {
                        title: '低保证号',
                        align: 'center',
                        key: 'ssca0014',
                        width: 120
                    },
                    {
                        title: '家庭编号',
                        align: 'center',
                        key: 'azdf0001',
                        width: 200
                    },
                    {
                        title: '受理编号',
                        align: 'center',
                        key: 'ssca0061',
                        width: 200
                    },
                    {
                        title: '家庭月总收入',
                        align: 'center',
                        key: 'ssca0049',
                        width: 120
                    },
                    {
                        title: '保障人口数',
                        align: 'center',
                        key: 'ssca0052',
                        width: 120
                    },
                    {
                        title: '家庭住址',
                        align: 'center',
                        key: 'azdf0008',
                        width: 200
                    },
                    {
                        title: '保障标准',
                        align: 'center',
                        key: 'ssca0042',
                        width: 120
                    },
                    {
                        title: '总金额',
                        align: 'center',
                        key: 'fullmoney',
                        width: 120
                    },
                    {
                        title: '批准时间',
                        align: 'center',
                        key: 'ssca0006',
                        width: 120,
                        render: (h, params) => {
                            let value = params.row.ssca0006
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '家庭基础收入',
                        align: 'center',
                        key: 'ssca0044',
                        width: 120
                    },
                    {
                        title: '家庭赡抚养费',
                        align: 'center',
                        key: 'ssca0045',
                        width: 120
                    },
                    {
                        title: '家庭财产',
                        align: 'center',
                        key: 'ssca0046',
                        width: 120
                    },
                    {
                        title: '家庭支出',
                        align: 'center',
                        key: 'ssca0047',
                        width: 120
                    },
                    {
                        title: '申请时间',
                        align: 'center',
                        key: 'ssca0003',
                        width: 120,
                        render: (h, params) => {
                            let value = params.row.ssca0003
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '变更时间',
                        align: 'center',
                        key: 'ssca0033',
                        width: 120,
                        render: (h, params) => {
                            let value = params.row.ssca0033
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '致贫原因',
                        align: 'center',
                        key: 'ssca0013',
                        width: 120
                    },
                    // {
                    //     title: '数据来源',
                    //     align: 'center',
                    //     key: 'ssca0032',
                    //     width: 120
                    // },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: 70,
                        fixed: 'right'
                    }
                ],
                ssca0011Check: true,
                temp_photo: '',
                upPicData: {},
                searchInfo: "",
                sq_disabled: false,
                jd_disabled: false,
                xq_disabled: false,
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss00016Service/getPageListForActing01"
                },
                formData: {},
                processData: {},
                familyMember: {},
                inputSsca0010: true,
                selectSsca0010: true,
                queryParam: {},
                visible: false,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: ""
            }
        },
        methods: {
            handleMaxSize() {
                this.$Message.error("文件过大, 请将文件控制在300KB以下再上传")
            },
            handleDetail(index) {
                let that = this;
                that.title = "详情";
                that.disableSubmit = true;
                // 查询该户所有家庭成员(保障/非保障, 不包含赡扶养人)
                postAction("/api/ac/subsistence/ss00016Service/findMember", that.tableData[index]).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {
                        that.familyMember = res.data.data;
                        this.Loading = false;
                    }
                }).finally(() => {
                });
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record);
                    that.temp_photo = that.formData.ssca0095;
                    if (that.formData.ssca0082 === '1') {
                        that.inputSsca0010 = true;
                        that.selectSsca0010 = false;
                    } else {
                        that.inputSsca0010 = false;
                        that.selectSsca0010 = true;
                    }
                    that.ssca0011Check = false;
                    // that.changeMember();
                } else {
                    that.visible = false
                }
            },
            handleEdit(index) {
                let that = this;
                that.ssca0011Check = true;
                that.disableSubmit = false;
                if (that.tableData[index].bpm_status != '100') {
                    this.$Message.error("业务正在办理中, 不允许修改银行账户信息");
                    return;
                }
                that.title = "银行账户信息编辑";
                that.Loading = true;
                // 删除12及17数据
                postAction("/api/ac/subsistence/ss00016Service/delete12And17", that.tableData[index]).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                        this.Loading = false;
                        return;
                    } else {
                    }
                }).finally(() => {
                });
                // 查询该户所有家庭成员(保障/非保障, 不包含赡扶养人)
                postAction("/api/ac/subsistence/ss00016Service/findMember", that.tableData[index]).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {
                        that.familyMember = res.data.data;
                        this.Loading = false;
                    }
                }).finally(() => {
                });
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record);
                    that.temp_photo = that.formData.ssca0095;
                    if (that.formData.ssca0082 === '1') {
                        that.inputSsca0010 = true;
                        that.selectSsca0010 = false;
                    } else {
                        that.inputSsca0010 = false;
                        that.selectSsca0010 = true;
                    }
                    that.changeMember();
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                for (let item of that.fieldsToValidate) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(that.rules[item][0].message);
                        }
                    })
                }
                let ssca0011 = that.formData.ssca0011;
                let ssca0011_check = that.formData.ssca0011_check;
                if (validate) {
                    // 验证银行卡号是否一致
                    if (ssca0011 != ssca0011_check) {
                        this.$Message.error("两次输入的银行卡号不一致");
                        return;
                    }
                    let that = this;
                    that.Loading = true;
                    that.visible = false;
                    let url = "/api/ac/subsistence/ss00016Service/updateData";
                    postAction(url, this.formData).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            this.modalFormOk();
                            this.visible = false;
                        }
                    }).finally(() => {
                        that.Loading = false;
                    });
                } else {
                    that.$Message.error(err_msg[0]);
                    return;
                }
            },
            loadExistData() { //输入身份证号码时载入数据
                let that = this
                let azcp0001 = that.formData.ssca0083
                azcp0001 = azcp0001.toUpperCase()
                that.formData.ssca0083 = azcp0001
                if (!IdentityCodeValid(azcp0001)) {
                    that.$Message.error('申请人身份证号码格式不合法')
                    that.formData.ssca0083 = null
                    return
                }
            },
            changeMember() {
                let that = this;
                if (that.formData.ssca0082 === '1') {
                    that.formData.ssca0010 = null;
                    that.formData.ssca0083 = null;
                    that.inputSsca0010 = true;
                    that.selectSsca0010 = false;
                } else if (that.formData.ssca0082 === '0') {
                    that.formData.ssca0010 = that.formData.azcp0003;
                    that.formData.ssca0083 = that.formData.azcp0001;
                    that.inputSsca0010 = false;
                    that.selectSsca0010 = true;
                } else {
                    that.formData.ssca0010 = null;
                    that.formData.ssca0083 = null;
                }
            },
            changeSsca0010() {
                let that = this;
                for (let item in that.familyMember) {
                    if (that.familyMember[item].azcp0003 == that.formData.ssca0010) {
                        that.formData.ssca0083 = that.familyMember[item].azcp0001;
                    }
                }
            },
            handleUpload(file) {
                let that = this;
                this.upPicData.fileName = '代领授权要件' + DateFormat(new Date(), 'YYYYMMDDhhmmss') + '.png';
                this.upPicData.filePath = 'subsistence/' + DateFormat(new Date(), 'YYYYMMDD') + '/';
                this.upPicData.fileName = this.upPicData.fileName + '.' + file.type.substring(6);
                const reader = new FileReader()
                reader.readAsDataURL(file)
                reader.onload = () => {
                    let index = reader.result.indexOf('base64,') + 7
                    that.upPicData.file = reader.result.substring(index)
                    var time = new Date().getTime()
                    this.timeStep = time.toString()
                    let param = {
                        filePath: that.upPicData.filePath,
                        fileName: that.upPicData.fileName,
                        timestamp: this.timeStep
                    }
                    if (param.fileName.length > 20) {
                        param.fileName = param.fileName.substring(0, 20) + '.png'
                    }
                    getSign(param).then(res => {
                        if (res.data.errcode === 0) {
                            createUpLoadFiles('/api/ac/hdplugin/ossService/uploadBase64', {
                                sign: res.data.data.sign,
                                timestamp: this.timeStep,
                                filePath: param.filePath,
                                fileName: param.fileName,
                                file: that.upPicData.file
                            }).then(res => {
                                if (res.data.errcode === 0) {
                                    let imgurl = res.data.data
                                    this.formData.ssca0095 = imgurl
                                    this.temp_photo = imgurl
                                } else {
                                    this.$Notice.error({title: '上传失败', desc: res.data.data.msg})
                                }
                            })
                        } else {
                            this.$Notice.error({title: '上传失败', desc: res.data.data.msg})
                        }
                    })
                }
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    /*.paging {*/
    /*    float: right;*/
    /*    margin-top: 10px;*/
    /*}*/

    /*/deep/ .ivu-upload-list {*/
    /*    display: none;*/
    /*}*/
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }
</style>
